package com.example.rabbitmq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class PaymentService {

    @RabbitListener(queues = RabbitmqConfig.paymentQueue)
    public void processPay(PaymentReq paymentReq, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws IOException {
        //System.out.println("收到支付请求.....");
        System.out.println("收到支付请求....."+paymentReq.getOrderId());

        try {
            Thread.sleep(1000);
            double random = Math.random();
            Boolean success = random>0.2;
            if (success){
                System.out.println(paymentReq.getUserId()+"支付成功"+paymentReq.getOrderId());
            }else {
                System.out.println(paymentReq.getUserId()+"支付失败"+paymentReq.getOrderId());
            }
            //
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            Thread.currentThread().interrupt();
            channel.basicNack(deliveryTag,false,true);
        }finally {

        }
    }

}
